<?php
/**
 * Created by PhpStorm.
 * User: khanhht
 * Date: 6/30/14
 * Time: 2:25 PM
 */

class UserController extends Zend_Controller_Action{

    public function init(){

    }

    public function changePassAction(){
        $this->userInfo = My_Auth::checkLogin();
        $form = new Default_Form_ChangePass();
        $flashHelper = $this->_helper->getHelper('FlashMessenger');
        if($this->_request->isPost()){
            $data = $this->_request->getPost();
            if($form->isValid($data)){
                $userModel = new Default_Model_User();
                $userData = $userModel->getDataById($this->userInfo['userId']);
                $params['oldPassword'] = md5($form->getValue('currPass'));
                $params['newPassword'] = md5($form->getValue('newPass'));
                if($userData['password'] != $params['oldPassword']){
                    $flashHelper->addMessage('error|Bạn nhập sai password hiện tại!');
                }else{
                    $userData->password = $params['newPassword'];
                    //save new password
                    $st = $userData->save();
                   
                    if($st){
                        $flashHelper->addMessage('success|Đổi password thành công!');
                    }else{
                        $flashHelper->addMessage('error|Có lỗi! Vui lòng thử lại sau!');
                    }
                }
                $this->_redirect('/user/change-pass');
            }
        }
        $this->view->messages = $flashHelper->getMessages();
        $flashHelper->clearMessages();
        $this->view->form = $form;
    }

} 